/**
 * add baby's info
 */

function firstBabyAdd(){
	$('div.firstBabyBook').on('click', function(){
		$('.addYourBaby').fadeIn('slow');
	});

	$('#cancle').on('click', function(){
		$('.addYourBaby').fadeOut('slow');
	});
}

function saveform() {
	var name = $('#name').val();
	var borndate = $('#borndate').val();			/* 태어난 날짜 */
	var bornweek = $('#bornweek').val();	/* 어머니뱃속에 있던 기간 */
	var sex = $('.genderRadio:checked').val();
	
	var headsize = $('#headsize').val();
	var height = $('#height').val();
	var weight = $('#weight').val();
	
	var deliveryinfo = $('.deliveryInfo:checked').val();	/* 자연분만, 제왕절개 */
	var birthtype = $('.birthType:checked').val();			/* 만삭, 조산 */
	var birthlocation = $('#birthlocation').val();
	
	$.ajax({
		url: '../baby/baby.action',
		type: 'POST',
		dataType: 'json',
		data: {
			name:name
			, borndate:borndate
			, bornweek:bornweek
			, sex:sex
			, headsize:headsize
			, height:height
			, weight:weight
			, deliveryinfo:deliveryinfo
			, birthtype:birthtype
			, birthlocation:birthlocation
		},
		success: saveComplete
	});

	function saveComplete(json){
		var loginId = json.loginId;
		$.ajax({
			url: "../baby/selectBabyList.action"
			, type: "POST"
			, dataType: "json"
			, data: {loginId: loginId}
			, success: function(json){
				var list = json.list;
				if(list.length == 1){
					var temp = $('div.hasNoBaby');
					temp.addClass('tabAndCalendar');
					temp.removeClass('hasNoBaby');
				}
				var html = '<div id="loading"><img src="../image/signup/ajax-loader2.gif"></div>';
				html += '<div id="tabs"><!-- 탭을 표현해주는 곳 -->';
				html += '<ul>';
				html += '</ul>';
				html += '<!-- 탭이 구현되는 곳: ajax로 불러오므로 div만 필 -->';
				html += '</div><div id="calendar"></div>';
				html += '<div class="addBabyMore"><i class="fa fa-plus-square-o"></i></div>';
				html += '<div class="addYourBaby option2"></div>';
				$('div.tabAndCalendar').html(html);
				$('div.addYourBaby').load('../jsp/account/babyInfoWrite.jsp');
				
				var html2 = '';
				for(var i = 0; i < list.length; i++){
					html2 += '<li onclick="javascript:loadingInfo('+(i+1)+', '+list[i].babynum+')">';
					html2 += '<a href="#tabs-'+(i+1)+'">'+list[i].name+'</a></li>';
				}
				$('#tabs ul').html(html2);
				
				var html3 = '';
				for(var i = 0; i < list.length; i++){
					html3 += '<div id="tabs-'+(i+1)+'" class="graphAll"></div>';
				}
				var tabs = $('#tabs');
				tabs.append(html3);
				tabs.tabs();
				loadingInfo(1, list[0].babynum);
				
				// load the calendar
				$('#calendar').eCalendar({
					onClickDay: function(){
						alert($('#calendar span.hb-day').text());
					}
				});
				$('.tabAndCalendar').fadeIn('slow');
				$('div.addYourBaby').hide();
				
				// button events
				$('div.addBabyMore').on('click', function(){
					$('div.addYourBaby').fadeIn();
				});
				window.location.reload();
			}
		});
	}
}